<template>
	<view class="b_width hui">
		<view @click="openImg">
			<swiper :style="{ height: 350 + 'px' }" :indicator-dots="true" :autoplay="true" :interval="3000"
				:duration="1000">
				<swiper-item v-for="(item,index) in info.productInfo.sliderImage" :key="index">
					<image :src="item" class="i_lun"></image>
				</swiper-item>
			</swiper>
		</view>
		<view class="info_box">
			<view class="name">{{info.productInfo.storeName}}</view>
			<view class="msg">{{info.productInfo.storeInfo}}</view>
			<view class="b_flex_c pbox">
				￥ {{info.productInfo.price}}<text class="unit">/{{info.productInfo.unitName}}</text>
			</view>
		</view>
		<view class="info_box">
			<view class="tab_box b_flex_cc">
				<u-tabs :current="current" @click="setTab" :list="tab" lineColor="#FF7133" lineWidth="40"
					:activeStyle="{color:'#FF7133',fontWeight:'bold'}" :inactiveStyle="{color: '#9296AF'}"
					itemStyle='height:80rpx;width:200rpx;'></u-tabs>
			</view>
			<view class="content" v-if="val==1">
				<u-parse v-if="info.productInfo.content" :content="info.productInfo.content"></u-parse>
				<empty v-else text="暂无详情"></empty>
			</view>
			<view class="content" v-else>
				<view v-if="list&&list.length>0">
					<view class="plInfo" v-for="(item,index) in list" :key="index">
						<view class="b_flex_c">
							<image :src="item.avatar" class="avatar"></image>
							<view>
								<view>{{item.nickname}}</view>
								<u-rate :count="5" v-model="item.score" activeColor='#ffca3e' :readonly='true'
									size="20"></u-rate>
							</view>
							<view class="time">{{$project.setTime(item.createTime,3)}}</view>
						</view>
						<view class="comment">{{item.comment}}</view>

					</view>
				</view>
				<empty v-else text="暂无评论"></empty>
			</view>
		</view>
		<view class="b_tc_ios" v-if="system=='ios'"></view>
		<view class="b_tc" v-else></view>
		<view :class="['b_flex_c','bottom_box',system=='ios'?'pm':'']">
			<view class="item" @click="$project.toTable('/pages/index/index')">
				<view class="iconfont iconshouye2-copy"></view>
				<view>首页</view>
			</view>
			<view class="item" @click="$project.toPath('/pages/shop/car')">

				<view class="iconfont icongouwuche21-copy"></view>
				<view>购物车</view>
				<view class="buyNum" v-if="buyNum">{{buyNum}}</view>
			</view>
			<view class="item" @click="setSc">
				<view class="iconfont icon-shoucang" v-if="!info.userCollect"></view>
				<view class="iconfont icon-shoucang1" v-else></view>
				<view>收藏</view>
			</view>
			<view class="btn car" @click="setOrder(1)">加入购物车</view>
			<view class="btn ju" @click="setOrder(2)">立即购买</view>
		</view>

		<u-popup round="20" mode="bottom" :show="show" :closeable="true" @close="show=false">
			<view class="po_box">
				<view class="b_flex">
					<image :src="info.productInfo.image" class="img"></image>
					<view class="p_left">
						<view class="p_name">{{info.productInfo.storeName}}</view>
						<view class="jf_box b_red">

							￥ {{info.productInfo.price}}<text class="unit">/{{info.productInfo.unitName}}</text>

						</view>
					</view>
				</view>
				<!-- <view class="pup_box">
					<view class="fl">规格</view>
					<view class="b_flex_c_w">
						<view v-for="item in info.skus" :class="[skus.id==item.id?'check_box':'','guige']"
							:key="item.id" @click="setSkus(item)">
							{{item.name}}
						</view>
					</view>
				</view> -->
				<view class="pup_box b_flex_c_s">
					<view class="fl">数量</view>
					<u-number-box v-model="num"></u-number-box>
				</view>
				<view class='btn_box p_btn' @click="submit">确定</view>
			</view>
		</u-popup>

	</view>
</template>

<script>
	import {
		goodsDeatil,
		goodsPl,
		goodsSc,
		goodsScs,
		addCar,
		carNum,
		yOrder
	} from "@/require/api.js";
	import empty from "@/components/empty.vue";
	export default {
		components: {
			empty
		},
		data() {
			return {
				current: 0,
				tab: [{
						name: '商品详情',
						val: 1
					},
					{
						name: '评论(0)',
						val: 2
					}
				],
				id: '',
				info: {},
				val: 1,
				list: [],
				params: {
					page: 1,
					limit: 10
				},
				total: '',
				show: false,
				type: '',
				num: 1,
				system: '',
				buyNum: ''

			}
		},
		onLoad(data) {
			this.system = uni.getSystemInfoSync().platform;
			if (data.id) {
				this.id = data.id;
				this.getInfo(data.id);
				this.getPl(data.id);
				this.getNum();
			}
		},
		onShow() {

		},
		onReachBottom() {
			if (this.list.length < this.total) {
				this.params.page++;
				goodsPl(this.params).then(res => {
					res.data.list.forEach((res) => {
						this.list.push(res);
					});
				});
			}
		},
		methods: {
			async getNum() {
				let {
					data
				} = await carNum({
					numType: true,
					type: 'total'
				});
				this.buyNum = data.count;
			},
			async submit() {

				if (this.type == 1) {
					let {
						data,
						code,
						message
					} = await addCar({
						cartNum: this.num,
						productId: this.id,
						productAttrUnique: this.info.productValue[this.info.productAttr[0].attrValues].id
					});

					if (code == 200) {
						this.$project.toast(message);
						this.show = false;
						this.getNum();
					}

				} else {

                 let {data,code,messgae}= await yOrder({preOrderType:'buyNow',orderDetails:[{attrValueId:this.info.productValue[this.info.productAttr[0].attrValues].id,productId:this.id,productNum:this.num}]});
                   if(code==200){
                   					  this.$project.toPath('/pages/shop/order?preOrderNo='+data.preOrderNo);
                   }
				}


			},
			setOrder(e) {
				this.type = e;
				this.show = true;
			},
			async setSc() {
				if (this.info.userCollect) {
					let {
						data,
						code
					} = await goodsScs(this.id);
					if (code == 200) {
						this.info.userCollect = false;
					}
				} else {
					let {
						data,
						code
					} = await goodsSc({
						id: this.id,
						category: 'store'
					});
					if (code == 200) {
						this.info.userCollect = true;
					}
				}

			},
			setTab(e) {
				console.log(e, this.current);
				this.val = e.val;
			},
			openImg() {
				uni.previewImage({
					urls: this.info.productInfo.sliderImage
				});
			},
			async getPl(id) {
				let {
					data
				} = await goodsPl(id, {
					type: 0
				});
				this.tab[1].name = '评论(' + data.total + ')';
				this.list = data.list;
				this.total = data.total;
			},
			async getInfo(id) {
				let {
					data
				} = await goodsDeatil(id);
				data.productInfo.sliderImage = JSON.parse(data.productInfo.sliderImage);
				this.info = data;

			}
		}
	}
</script>

<style lang="scss" scoped>
	.i_lun {
		width: 100%;
		height: 100%;
	}

	.info_box {
		box-sizing: border-box;
		padding: 20rpx 30rpx;
		background-color: white;
		margin-bottom: 20rpx;
	}

	.name {
		font-size: 36rpx;
		font-weight: bold;
	}

	.msg {
		font-size: 28rpx;
		color: #737373;
		margin: 20rpx 0;
	}

	.pbox {
		font-size: 46rpx;
		color: #F01D1D;
		font-weight: bold;
	}

	.content {
		margin-top: 20rpx;
		min-height: 690rpx;
	}

	.avatar {
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
		margin-right: 20rpx;
	}

	.time {
		margin-left: auto;
		font-size: 28rpx;
	}

	.comment {
		margin-top: 20rpx;
	}

	.plInfo {
		box-sizing: border-box;
		padding: 30rpx 0 30rpx 0;
		border-bottom: 1rpx solid #eee;
	}

	.iconfont {
		font-size: 40rpx;
	}

	.icon-shoucang1 {
		color: #FF7133;
	}

	.bottom_box {
		width: 100%;
		position: fixed;
		left: 0;
		bottom: 0;
		background-color: white;
		box-sizing: border-box;

		padding-top: 6rpx;
		padding-right: 10rpx;
		padding-bottom: 6rpx;
	}

	.item {
		width: 15%;
		text-align: center;
		font-size: 26rpx;
		position: relative;
	}

	.btn {
		width: 27%;
		text-align: center;
		color: white;
		font-size: 30rpx;
		height: 78rpx;
		line-height: 78rpx;
		border-radius: 45rpx;
	}

	.car {
		background-color: #F3CB3D;
	}

	.ju {
		background-color: #FF7133;
		margin-left: 1%;
	}

	.b_tc {
		height: 91rpx;
	}

	.b_tc_ios {
		height: 135rpx;
	}

	.po_box {
		box-sizing: border-box;
		padding: 60rpx 20rpx;
		background-color: white;
		border-radius: 20rpx;
	}

	.img {
		width: 200rpx;
		height: 200rpx;
		margin-right: 20rpx;
	}

	.p_name {
		margin-bottom: 20rpx;
	}


	.fl {
		margin: 20rpx 0;
		font-size: 26rpx;
		font-weight: bold;
	}

	.guige {
		text-align: center;
		height: 48rpx;
		line-height: 48rpx;

		border: 1rpx solid #9A9A9A;
		border-radius: 25rpx;
		box-sizing: border-box;
		padding: 0 40rpx;
		color: #9A9A9A;
		font-size: 22rpx;
		margin: 0 30rpx 0 0;
	}

	.check_box {

		border: 1rpx solid red;
		color: white;
		background-color: red;
	}

	.pup_box {
		margin-top: 30rpx;
	}

	.p_btn {
		margin: 120rpx 0 0 0;
		padding: 20rpx 0;
	}

	.s_n {
		max-width: 63%;
	}

	.s_k {
		max-width: 27%;
	}

	.ch_box {
		width: 95%;
	}

	.p_left {
		width: 65%;
	}

	.btn_box {
		//margin-left: 30rpx;
		text-align: center;
		width: 100%;
		background-color: #FF4E1B;
		color: white;
		box-sizing: border-box;
		padding: 25rpx 0;
		border-radius: 50rpx;
		font-size: 33rpx;
		font-weight: bold;
	}

	.pm {
		padding-bottom: 50rpx;
	}

	.buyNum {
		
		text-align: center;
		position: absolute;
		right: 20rpx;
		top: 0;
		width: 30rpx;
		height: 30rpx;
		line-height: 30rpx;
		font-size: 14rpx;
		font-size: bold;
		border-radius: 50%;
		background: #FF7133;
		color: white;
	}
</style>